<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class User extends Model
{
    use SoftDelete;

    // 设置当前模型对应的完整数据表名称
    protected $name = 'tb02_user';

    // 设置主键
    protected $pk = 'user_id';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    protected $deleteTime = 'deleted_at';
    protected $dateFormat = 'Y-m-d H:i:s';

    // 字段类型转换
    protected $type = [
        'points'           => 'integer',
        'status'           => 'integer',
        'total_sign_days'  => 'integer',
        'continuity_days'  => 'integer',
    ];

    // 隐藏字段
    protected $hidden = ['password', 'deleted_at'];

    // 状态枚举
    const STATUS_NORMAL = 1; // 正常
    const STATUS_LOCKED = 2; // 锁定
    const STATUS_LOGOUT = 3; // 注销

    /**
     * 密码加密
     */
    public function setPasswordAttr($value)
    {
        return password_hash($value, PASSWORD_DEFAULT);
    }

    /**
     * 最后登录时间处理
     */
    public function setLastAtAttr()
    {
        return date('Y-m-d H:i:s');
    }
    // 在模型中添加状态获取方法
    public function getStatusTextAttr()
    {
        $status = [
            self::STATUS_NORMAL => '正常',
            self::STATUS_LOCKED => '锁定',
            self::STATUS_LOGOUT => '注销'
        ];
        return $status[$this->status] ?? '未知';
    }

}
